﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace GameServer
{
    using Server = GameNetworking.Server;
    using GameNetworking;
using System.Threading;

    public partial class ServerForm : Form
    {
        private delegate void UpdateStatusCallback(string strMessage);

        Server gameServer;
        Thread updateServerMessages;
        object state;
        private IAsyncResult result;
        
        public ServerForm()
        {
            InitializeComponent();
            updateServerMessages = new Thread(new ThreadStart(UpdateMessages));
            updateServerMessages.Start();
            btnStopServer.Enabled = false;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnStartServer_Click(object sender, EventArgs e)
        {
            gameServer = new Server(new IPEndPoint(IPAddress.Parse("127.0.0.1"),7777).Address);
            Server.StatusChanged += new StatusChangedEventHandler(Server_StatusChanged);
            gameServer.StartListening();
            tbServerConsole.AppendText("Waiting for connections...\r\n");
            btnStartServer.Enabled = false;
            btnStopServer.Enabled = true;
        }

        void Server_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
        }

        private void UpdateStatus(string strMessage)
        {
            // Updates the log with the message
            tbServerConsole.AppendText(strMessage + "\r\n");
        }

        private void btnClientStart_Click(object sender, EventArgs e)
        {
            ClientForm cf = new ClientForm();
            cf.Show();
        }

        private void UpdateMessages()
        {
            //gameServer.ClientMessages.ForEach((message) => tbServerConsole.AppendText(message));
        }

        private void UpdateClientList()
        {
            if (lbClients.Items.Count > 0)
                lbClients.Items.Clear();
            gameServer.ClientList.ForEach((client) => lbClients.Items.Add(client));
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateMessages();
            UpdateClientList();
        }

        private void btnStopServer_Click(object sender, EventArgs e)
        {
            //gameServer.Stop();
            btnStartServer.Enabled = true;
            btnStopServer.Enabled = false;
        }



        //private AsyncCallback GotMessage()
        //{
        //    return new AsyncCallback(ProcessResult);
        //}

        //private void ProcessResult(object obj)
        //{
        //    MessageBox.Show("Processed Result!\n"+obj.ToString());
        //}
    }
}
